package com.vvv.designpattern.structualpatterns;

/**
 * @author vvv
 * @date 2023-11-13 11 44
 * discription 以redis的连接伪代码来实现
 */
public class FactoryMethod {
    public static void main(String[] args) {
        /**
         * 工厂方法模式就是符合开闭原则的工厂模式
         * 先抽象出接口，再让子类去具体的实现，这样就可以满足扩展性了，
         */
        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
        RedisConnection connection1 = lettuceConnectionFactory.getConnection();
        connection1.connection();

        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        RedisConnection connection2 = jedisConnectionFactory.getConnection();
        connection2.connection();
    }
}
// 抽象
interface RedisConnection {
    void connection();
}
// 具体
class LettuceConnection implements RedisConnection {

    @Override
    public void connection() {
        System.out.println("Lettuce 连接");
    }
}
// 具体
class JedisConnection implements RedisConnection {

    @Override
    public void connection() {
        System.out.println("Jedis 连接");
    }
}
// 抽象
interface RedisConnectionFactory {
    RedisConnection getConnection();
}
// 具体
class JedisConnectionFactory implements RedisConnectionFactory {

    @Override
    public RedisConnection getConnection() {
        return new JedisConnection();
    }
}
// 具体
class LettuceConnectionFactory implements RedisConnectionFactory {

    @Override
    public RedisConnection getConnection() {
        return new LettuceConnection();
    }
}

